using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using DTO;
using System.Data;
using System.Collections;

namespace DAOLayer
{
    public class QuaTrinhMuonDao
    {
        public void ThemQuaTrinhMuon(QuaTrinhMuonDto qtm)
        {
            OleDbConnection cn = Database.ConnectionData();
            string str = "Insert into QuaTrinhMuon (Isbn,MaCuonSach,MaDocGia,NgayMuon,NgayTra,TienPhat) values(?,?,?,?,?,?)";
            OleDbCommand cmd = new OleDbCommand(str, cn);
            cmd.Parameters.Add("@Isbn", OleDbType.Integer);
            cmd.Parameters["@Isbn"].Value = qtm.Isbn;

            cmd.Parameters.Add("@MaCuonSach", OleDbType.WChar);
            cmd.Parameters["@MaCuonSach"].Value = qtm.MaCuonSach;

            cmd.Parameters.Add("@MaDocGia", OleDbType.WChar);
            cmd.Parameters["@MaDocGia"].Value = qtm.MaDocGia;

            cmd.Parameters.Add("@NgayMuon", OleDbType.Date);
            cmd.Parameters["@NgayMuon"].Value = qtm.NgayMuon;

            cmd.Parameters.Add("@NgayTra", OleDbType.Date);
            cmd.Parameters["@NgayTra"].Value = qtm.NgayTra;

            cmd.Parameters.Add("@TienPhat", OleDbType.Double);
            cmd.Parameters["@TienPhat"].Value = qtm.TienPhat;


            cmd.ExecuteNonQuery();
            str = "Select @@IDENTITY";
            cmd = new OleDbCommand(str, cn);
            qtm.Isbn = (int)cmd.ExecuteScalar();
            cn.Close();
        }

        public void CapNhatBangQuaTrinhMuon(DataTable dt)
        {
            OleDbConnection cn;
            cn = Database.ConnectionData();
            string strSQL;
            strSQL = "Select * From QuaTrinhMuon";
            OleDbDataAdapter da;
            da = new OleDbDataAdapter(strSQL, cn);
            OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
            da.Update(dt);
            cn.Close();
        }

        public DataTable LayBangQuaTrinhMuon()
        {
            DataTable dt = new DataTable();
            OleDbConnection cn = Database.ConnectionData();
            string strSQL = "Select * From QuaTrinhMuon";
            OleDbDataAdapter da;
            da = new OleDbDataAdapter(strSQL, cn);
            da.Fill(dt);
            cn.Close();
            return dt;
        }

        public ArrayList LayDanhSachQuaTrinhMuon()
        {
            ArrayList ds = new ArrayList();
            OleDbConnection cn = Database.ConnectionData();
            string strSQL;
            strSQL = "Select * From QuaTrinhMuon";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            OleDbDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                QuaTrinhMuonDto qtmDto = new QuaTrinhMuonDto();
                qtmDto.Isbn = (int)dr["Isbn"];
                qtmDto.MaCuonSach = (string)dr["MaCuonSach"];
                qtmDto.MaDocGia = (string)dr["MaDocGia"];
                qtmDto.NgayMuon = (DateTime)dr["NgayMuon"];
                qtmDto.NgayTra = (DateTime)dr["NgayTra"];
                qtmDto.TienPhat = (double)dr["TienPhat"];
                ds.Add(qtmDto);
            }

            dr.Close();
            cn.Close();
            return ds;
        }

    }
}
